package ru.niir.goodfil.ui.utils.excel.unload;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import ru.niir.goodfil.ui.models.DetailedAbsenceByClientModel;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Волков Олег on 27.01.15.
 */
public class GoodAbsenceUnloading {

    public static void goodAbsenceUnloadingByClient(List<DetailedAbsenceByClientModel> list, String companyName, String periodName){

        Collections.sort(list, new Comparator<DetailedAbsenceByClientModel>() {
            @Override
            public int compare(DetailedAbsenceByClientModel o1, DetailedAbsenceByClientModel o2) {
                return o1.getGoodCode().compareTo(o2.getGoodCode());
            }
        });

        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet sheet = hssfWorkbook.createSheet("0");
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 2));
        int i = 0;
        int j = 0;
        int total = 0;
        long totalSum = 0;

        HSSFRow client = sheet.createRow(i);
        Cell name = client.createCell(0);
        name.setCellValue(companyName);
        i++;

        HSSFRow header = sheet.createRow(i);
        Cell date = header.createCell(0);
        date.setCellValue(periodName);
        i++;

        HSSFRow tableHeader = sheet.createRow(i);
        Cell code = tableHeader.createCell(j);
        code.setCellValue("Код товара");
        j++;

        Cell goodCount = tableHeader.createCell(j);
        goodCount.setCellValue("Кол-во, шт.");
        sheet.autoSizeColumn(j);
        j++;

        Cell inquriesCount = tableHeader.createCell(j);
        inquriesCount.setCellValue("Кол-во запросов");
        sheet.autoSizeColumn(j);
        j++;

        Cell sum = tableHeader.createCell(j);
        inquriesCount.setCellValue("Сумма");
        sheet.autoSizeColumn(j);
        i++;

        for(DetailedAbsenceByClientModel model : list){
            j = 0;
            totalSum += model.getSum();
            total += model.getGoodCount();
            HSSFRow row = sheet.createRow(i);

            Cell _goodCode = row.createCell(j);
            _goodCode.setCellValue(model.getGoodCode());
            sheet.autoSizeColumn(j);
            j++;

            Cell _goodCount = row.createCell(j);
            _goodCount.setCellValue(model.getGoodCount());
            j++;

            Cell _inquriesCount = row.createCell(j);
            _inquriesCount.setCellValue(model.getInquriesCount());
            i++;

            Cell _sum = row.createCell(j);
            _sum.setCellValue(model.getSum());
        }
        j = 0;

        HSSFRow totalRow = sheet.createRow(i);

        Cell totalCell = totalRow.createCell(j);
        totalCell.setCellValue("Итого");
        j++;

        Cell _totalCell = totalRow.createCell(j);
        _totalCell.setCellValue(total);
        j++;

        Cell _totalSum = totalRow.createCell(j);
        _totalSum.setCellValue(totalSum);

        try {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            ExternalContext externalContext = facesContext.getExternalContext();
            HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=\"report.xls\"");
            // Запись
            hssfWorkbook.write(response.getOutputStream());
            // Закрытие потока
            facesContext.responseComplete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
